﻿/* 
 * Server.cs
 * Spring Lab 4 2009
 * Bob Waters
 */

namespace Server
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;
    using System.Xml;
    using System.Xml.Serialization;
    using System.IO;

    /// <summary>
    /// The main class for our server application.  Listens for client connections.
    /// </summary>
    public class Server
    {
        #region attributes
        /// <summary>
        /// the port number of the server
        /// </summary>
        private int port;

        /// <summary>
        /// reference to the manager of all users
        /// </summary>
        private UserManager manager;

        #endregion

        #region constructors

        /// <summary>
        /// Initializes a new instance of the Server class
        /// </summary>
        /// <param name="pt">the port number for the server</param>
        public Server(int pt)
        {
            this.port = pt;

            XmlSerializer deserial = new XmlSerializer(typeof(UserManager));
            try
            {
                TextReader read = new StreamReader("users.xml");
                this.manager = (UserManager)deserial.Deserialize(read);
                read.Close();
            }
            catch (System.IO.FileNotFoundException)
            {
                this.manager = new UserManager();
            }
        }

        #endregion

        #region instance methods

        /// <summary>
        /// This method is called to start up the tcp socket, listen for
        /// connections, and create the ClientHandlers to handle the 
        /// new connection.
        /// </summary>
        public void Run()
        {
            // DONE: Put code here to create a server socket and start it up
            bool running = true;
            System.Console.WriteLine("Starting Server on: " + this.port);
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(ip, port);
            listener.Start();

            while (running)
            {
                // DONE: put code here to accept connections and create a new ClientHandler object
                Socket s = listener.AcceptSocket();
                ClientHandler client = new ClientHandler(s, manager);
                Thread clientthread = new Thread(new ParameterizedThreadStart(client.HandleClient));
                clientthread.Start(manager);
                System.Console.WriteLine("Connected to client: " + s.Handle);
            }
        }

        #endregion
        /// <summary>
        /// Main method for server application.  Gets the port
        /// from the command line and creates a new instance
        /// of the server class.  Then tells the instance to 
        /// start up the server.
        /// </summary>
        /// <param name="args">The command line arguments</param>
        public static void Main(string[] args)
        {
            int port = 45678;
            if (args != null && args.Length > 0)
                port = Int32.Parse(args[0]);
            Server s = new Server(port);
            s.Run();
        }
    }
}
